package com.yuxin.tongyi.lsll.sub;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yuxin.tongyi.lsll.R;
import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.pojo.Student;
import com.yuxin.tongyi.lsll.pojo.StudentInfo;
import com.yuxin.tongyi.lsll.thread.GetStudentDataThread;
import com.yuxin.tongyi.lsll.util.PubUtil;

public class ShowStudentDialog extends AlertDialog implements OnClickListener {
	private final static String TAG = "ShowStudentActivity";
	
	private ListView lvStudent;
	private Button btnSelect, btnBack;
	
	private List<Student> studentlist;  //学生数据
	private MyHandler myHandler = null;
	private GetStudentDataThread stuThread;
	
	private String loginName = "";
	private String passWord = "";
	private Context context;
	private int type;       //0表示拍照上传
	private String classId; // 班级id
	
	//private ArrayList<String> SelectedIdList;  //选中列表
	private ArrayList<Boolean> SelectedIdList;
	private SelectStudent mSelectStudent;  //回调接口
	private String stus = "";   //选中的学生Id 多个id逗号分隔
	
	protected ShowStudentDialog(Context context,int type, String classid, String loginname, String password) {
		super(context);
		this.context = context;
		this.type = type;
		this.classId = classid;
		this.loginName = loginname;
		this.passWord = password;
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_student);
		
		initView();
		initData();
		
		SelectedIdList = new ArrayList<Boolean>();
		myHandler = new MyHandler();
		stuThread = new GetStudentDataThread(context, myHandler);
		new Thread(stuThread).start();
	}
	
	public void initView() {
		lvStudent = (ListView) findViewById(R.id.list_student_lvstudent);
		btnSelect = (Button) findViewById(R.id.list_student_btnselect);
		btnSelect.setOnClickListener(this);
		btnBack = (Button) findViewById(R.id.list_student_btnback);
		btnBack.setOnClickListener(this);
	}
	
	public void initData() {
		if(type==0) {
			//选择的学生 多个逗号分隔
		    stus = CaremaPhotoUploadActivity.studentIds;
		}
		
	}
	
	/**
	 * 加载学生数据
	 * @return
	 */
	public void setStudentData(StudentInfo Info) {
		studentlist = Info.getStudent();
		
		String[] stuId = null;
		List<String> stuList = null;
		if(stus!=null && !"".equals(stus)){
			stuId = stus.split(",");
			stuList = Arrays.asList(stuId);
		}
		
		//给选择状态列表赋值
		for(int i=0; i<studentlist.size(); i++){
			String studentId = studentlist.get(i).getStudentId();
			if(stuList!=null && stuList.contains(studentId)){
				SelectedIdList.add(i, true);
			}else{
				SelectedIdList.add(i, false);
			}
		}
		
		CustomAdapter chadapter = new CustomAdapter(context, studentlist);
		lvStudent.setAdapter(chadapter);
	}
	
	class MyHandler extends Handler {

		public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Bundle bundle = msg.getData();
				
				switch (msg.what) {
				case Constants.TH_STUDENT_ON:
					StudentInfo studentInfo = (StudentInfo) bundle.getSerializable(Constants.STREAM_STUDENT_OBJECT);
					setStudentData(studentInfo);
					
					break;
				// 暂无数据
				case Constants.TH_STUDENT_EMPTY:
					PubUtil.showToast(context, Constants.MSG_NODATA);
					break;
				//读取STUDENT失败，加载缓存
				case Constants.TH_STUDENT_OFF:
					PubUtil.showToastLong(context, Constants.MSG_READCACHE);
					StudentInfo studentInfo1 = (StudentInfo) bundle.getSerializable(Constants.STREAM_STUDENT_OBJECT);
					setStudentData(studentInfo1);
					break;
				}
		}
	}
	
	public final class ViewHolder {
		public TextView tvid;
		public TextView tvname;
		public CheckBox chkselected;
	}
	
	class CustomAdapter extends BaseAdapter{
		
		private List<Student> stulist;
		private Context context;
		private LayoutInflater mInflater;

		public CustomAdapter(Context context, List<Student> stulist){
			this.context = context;
			this.mInflater = LayoutInflater.from(context);
			this.stulist = stulist;
		}
		
		@Override
		public int getCount() {
			return stulist.size();
		}

		@Override
		public Object getItem(int position) {
			return stulist.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			ViewHolder holder = null;
			final int p = position;
			if (view == null) {
				holder = new ViewHolder();
				view = mInflater.inflate(R.layout.list_student_item, null);
				holder.tvname = (TextView) view.findViewById(R.id.list_student_item_tvname);
				holder.chkselected = (CheckBox) view.findViewById(R.id.list_student_item_chkselect);
				holder.tvid = (TextView) view.findViewById(R.id.list_student_item_tvid);
				view.setTag(holder);
			}else{
				holder = (ViewHolder) view.getTag();
			}
			
			holder.chkselected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					SelectedIdList.set(p, isChecked);
				}
			});
			//赋值
			final String StudentId = stulist.get(position).getStudentId();
			final String StudentName = stulist.get(position).getStudentName();
			final boolean StudentFlag = SelectedIdList.get(position);
			holder.tvid.setText(StudentId);
			holder.tvname.setText(StudentName);
			holder.chkselected.setChecked(StudentFlag);
			
			Log.i(TAG, "--------------------------------------------------------------");
			Log.i(TAG, position + " position():");
			Log.i(TAG, StudentName + " StudentName():");
			Log.i(TAG, StudentFlag + " StudentFlag():");
			Log.i(TAG, "--------------------------------------------------------------");
			
			return view;
		}
	}

	/**
	 * button点击事件 提示信息 返回主界面
	 */
	public void onClick(View v) {
		String studentIds = "";
		String studentNames = "";
		for(int i=0; i<SelectedIdList.size(); i++){
			if(SelectedIdList.get(i)){
				if("".equals(studentIds)){
					studentIds = studentlist.get(i).getStudentId();
				}else{
					studentIds =  studentIds + "," + studentlist.get(i).getStudentId();
				}
				
				if("".equals(studentNames)){
					studentNames = studentlist.get(i).getStudentName();
				}else{
					studentNames =  studentNames + "," + studentlist.get(i).getStudentName();
				}
			}
		}
		
		if(type==0) {
			  CaremaPhotoUploadActivity.studentIds = studentIds;
		}
		
		if(mSelectStudent!=null){
			mSelectStudent.setSelect(studentNames);
		}
		this.cancel();
	}
	
	public interface SelectStudent{
		public void setSelect(String StuStr);
	}
	
	
	public void setStudents(SelectStudent iStu){
		mSelectStudent = iStu;
	}
}
